/**
 * 
 */
package com.planner.core.service.rest.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

import com.google.appengine.repackaged.com.google.common.base.Predicate;
import com.google.appengine.repackaged.com.google.common.collect.Collections2;
import com.india.metro.delhi.DelhiMetroNode;
import com.planner.core.service.rest.StopNamePredicate;
import com.planner.core.service.rest.core.RestListWrapper;
import com.planner.core.service.rest.vo.Stop;
import com.planner.core.shared.util.StringUtil;

/**
 * @author gautasa
 * 
 */
@Path("/search/")
public class RestSearchService {
	
	private static final Log LOG = LogFactory.getLog(RestSearchService.class);
	
	@GET
	@Produces({MediaType.APPLICATION_XML})
	@Path("/stop")
	public List<Stop> search(@QueryParam ("searchStr") String searchStr) {
		if(StringUtil.isNullOrBlank(searchStr)){
			return new ArrayList<Stop>(0);
		}
		LOG.info("Search stop performed with query string: "+searchStr);
		Predicate<Stop> predicate = new StopNamePredicate(searchStr);
		List<Stop> filtered = new ArrayList<Stop>(Collections2.filter(getAllStops(), predicate));
		Collections.sort(filtered);
		return filtered;
	}
	
	@GET
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	@Path("/route")
	public RestListWrapper<String> search(@QueryParam("from") String from, @QueryParam("to") String to){
		List<String> nodes = new ArrayList<String>();
		nodes.add(DelhiMetroNode.VAISHALI.toString());
		return new RestListWrapper<String>(nodes);
	}
	
	private List<Stop> getAllStops() {
		List<Stop> stops = new ArrayList<Stop>();
		for(com.sangharsh.core.algo.StopNode node: com.sangharsh.core.algo.StopNode.values()){
			if(node.isActive())
				stops.add(new Stop(node.getId(), node.getName(), node.getCode(), node.getLocation().toString()));
		}
		return stops;
	}
	
}
